package com.wangshuqi.excel;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelTest {

    public static void main(String[] args) throws IOException {
        Workbook workbook = new SXSSFWorkbook();
        // 创建sheet
        Sheet sheet = workbook.createSheet();

        Font titleFont = workbook.createFont();
        titleFont.setBold(true); // 是否加粗
        titleFont.setFontHeightInPoints((short) 16);
        CellStyle titleStyle = workbook.createCellStyle();
        titleStyle.setBorderBottom(BorderStyle.THIN);
        titleStyle.setBorderLeft(BorderStyle.THIN);
        titleStyle.setBorderTop(BorderStyle.THIN);
        titleStyle.setBorderRight(BorderStyle.THIN);
        titleStyle.setAlignment(HorizontalAlignment.CENTER);//增加水平居中样式-old
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 设置垂直居中
        titleStyle.setFont(titleFont);
        // 创建标题行
        Row titleRow = sheet.createRow(0);
        titleRow.setHeightInPoints(24); // 设置行高为30像素
        Cell cell = titleRow.createCell(0);
        cell.setCellValue("诉讼类法律服务评价表");
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 10));
        cell.setCellStyle(titleStyle);

        // 创建表头行
        Row headerRow = sheet.createRow(1);
        Font headerFont = workbook.createFont();
        headerFont.setBold(true); // 是否加粗
        headerFont.setFontHeightInPoints((short) 14);
        CellStyle headerStyle = workbook.createCellStyle();
        // 边框设置
        headerStyle.setBorderBottom(BorderStyle.THIN);
        headerStyle.setBorderLeft(BorderStyle.THIN);
        headerStyle.setBorderTop(BorderStyle.THIN);
        headerStyle.setBorderRight(BorderStyle.THIN);
        headerStyle.setAlignment(HorizontalAlignment.CENTER);//增加水平居中样式-old
        headerStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 设置垂直居中
        headerStyle.setFont(headerFont);// 引用字体样式
        // 设置列头信息
        Cell headerRowCell0 = headerRow.createCell(0);
        headerRowCell0.setCellValue("序号");
        headerRowCell0.setCellStyle(headerStyle);
        sheet.setColumnWidth(headerRowCell0.getColumnIndex(), 10 * 256); // 设置第i列的列宽为 n * 256
        Cell headerRowCell1 = headerRow.createCell(1);
        headerRowCell1.setCellValue("项目名称");
        headerRowCell1.setCellStyle(headerStyle);
        sheet.setColumnWidth(headerRowCell1.getColumnIndex(), 20 * 256); // 设置第i列的列宽为 n * 256
        Cell headerRowCell2 = headerRow.createCell(2);
        headerRowCell2.setCellValue("案号");
        headerRowCell2.setCellStyle(headerStyle);
        sheet.setColumnWidth(headerRowCell2.getColumnIndex(), 25 * 256); // 设置第i列的列宽为 n * 256
        Cell headerRowCell3 = headerRow.createCell(3);
        headerRowCell3.setCellValue("主办律师");
        headerRowCell3.setCellStyle(headerStyle);
        sheet.setColumnWidth(headerRowCell3.getColumnIndex(), 20 * 256); // 设置第i列的列宽为 n * 256
        Cell headerRowCell4 = headerRow.createCell(4);
        headerRowCell4.setCellValue("律师事务所");
        headerRowCell4.setCellStyle(headerStyle);
        sheet.setColumnWidth(headerRowCell4.getColumnIndex(), 30 * 256); // 设置第i列的列宽为 n * 256
        Cell headerRowCell5 = headerRow.createCell(5);
        headerRowCell5.setCellValue("评分内容");
        headerRowCell5.setCellStyle(headerStyle);
        sheet.setColumnWidth(headerRowCell5.getColumnIndex(), 20 * 256); // 设置第i列的列宽为 n * 256
        Cell headerRowCell6 = headerRow.createCell(6);
        headerRowCell6.setCellStyle(headerStyle);
        sheet.setColumnWidth(headerRowCell6.getColumnIndex(), 20 * 256); // 设置第i列的列宽为 n * 256
        Cell headerRowCell7 = headerRow.createCell(7);
        headerRowCell7.setCellStyle(headerStyle);
        sheet.setColumnWidth(headerRowCell7.getColumnIndex(), 20 * 256); // 设置第i列的列宽为 n * 256
        sheet.addMergedRegion(new CellRangeAddress(1, 1, headerRowCell5.getColumnIndex(), headerRowCell5.getColumnIndex() + 2));
        Cell headerRowCell8 = headerRow.createCell(8);
        headerRowCell8.setCellValue("得分");
        headerRowCell8.setCellStyle(headerStyle);
        sheet.setColumnWidth(headerRowCell8.getColumnIndex(), 20 * 256); // 设置第i列的列宽为 n * 256
        Cell headerRowCell9 = headerRow.createCell(9);
        headerRowCell9.setCellValue("合计得分");
        headerRowCell9.setCellStyle(headerStyle);
        sheet.setColumnWidth(headerRowCell9.getColumnIndex(), 20 * 256); // 设置第i列的列宽为 n * 256
        Cell headerRowCell10 = headerRow.createCell(10);
        headerRowCell10.setCellValue("备注");
        headerRowCell10.setCellStyle(headerStyle);
        sheet.setColumnWidth(headerRowCell10.getColumnIndex(), 30 * 256); // 设置第i列的列宽为 n * 256

        // 导出
        File file = new File("D:\\Oneself_Project\\诉讼类法律服务评价表.xls");
        FileOutputStream outputStream = new FileOutputStream(file);
        workbook.write(outputStream);
        outputStream.close();
        System.out.println("导出成功");
    }
}
